package com.xteam.youin_supplier.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.JsonSyntaxException;
import com.xteam.youin_supplier.IndexActivity;
import com.xteam.youin_supplier.MainActivity;
import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.config.Config;
import com.xteam.youin_supplier.config.GlobalData;
import com.xteam.youin_supplier.config.RequestAddr;
import com.xteam.youin_supplier.model.BaseModel;
import com.xteam.youin_supplier.model.BeddingListItem;
import com.xteam.youin_supplier.model.UserInfo;
import com.xteam.youin_supplier.util.MethedUtil;
import com.xteam.youin_supplier.util.http.AjaxCallBack;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * 登录
 */
@EFragment(R.layout.fragment_login)
public class LoginFragment extends BaseFragment {

    @ViewById(R.id.userinfo)
    EditText userName;

    @ViewById(R.id.userpass)
    EditText userPass;

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;//获取编辑器

    String username;
    String password;

    @AfterViews
    @Override
    void initView() {
        sharedPreferences = IndexActivity.getInstace().getSharedPreferences("YOUIN_OPERATORS", Context.MODE_PRIVATE); //私有数据
        editor = sharedPreferences.edit();
        username = sharedPreferences.getString("username", "");
        password = sharedPreferences.getString("password", "");
        if (!username.equals("") && !password.equals("")) {
            userName.setText(username);
            userPass.setText(password);
        }
    }

    @Click({R.id.login_btn})
    @Override
    void Click(View view) {
        switch (view.getId()){
            case R.id.login_btn:
                login();
            break;
        }

    }


    //登录
    private void login() {
        if (!checValidate()) {
            return;
        }

        requestParams = "account=" + userName.getText().toString().trim()
                + "&password=" + userPass.getText().toString().trim();
        requestUrl = RequestAddr.login + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                dealResponse(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Toast.makeText(IndexActivity.getInstace(), "网络不稳定，请稍后再试", Toast.LENGTH_SHORT).show();
                super.onFailure(t, errorNo, strMsg);
            }


        });
    }

    BaseModel baseModel;
    private void dealResponse(Object object) {
        try {
            GlobalData.userInfo = gson.fromJson(object.toString(), UserInfo.class);
        } catch (JsonSyntaxException e) {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
            Toast.makeText(getActivity(),baseModel.getMsg(), Toast.LENGTH_SHORT).show();
            return;
        }
        editor.putString("username", userName.getText().toString().trim());
        editor.putString("password", userPass.getText().toString().trim());
        editor.commit();
        doInBackground();
    }
    //验证合法性
    private boolean checValidate() {
        if (userName.getText().toString().trim().equals("") || userPass.getText().toString().trim().equals("")) {
            Toast.makeText(getActivity(), getString(R.string.username_uaerpass_empty), Toast.LENGTH_SHORT).show();
            return false;
        }
        if (MethedUtil.isCheckedSpeChar(getActivity(), userName.getText().toString())
                ||MethedUtil.isCheckedSpeChar(getActivity(), userPass.getText().toString())) {
            return false;
        }
        return true;
    }

    @Background(delay = 0)
    void doInBackground(){
        Intent intent = new Intent();
        intent.setClass(IndexActivity.getInstace(), MainActivity.class);
        startActivity(intent);
        IndexActivity.getInstace().finish();
    }
}
